package com.tjvib.util.Manager;

import com.tjvib.util.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class UserManager {
    private final static UserManager instance = new UserManager();

    private int userId = 1;
    private String username;
    private String phoneNumber;
    private String image;
    private boolean isVip;
    private boolean enabled = true;
    private int collectLocalTrial;
    private int collectLpmsTrial;
    private int analysisTrial;
    private double balance;
    private static String token;

    private UserManager() {
    }

    public static UserManager getInstance() {
        return instance;
    }

    public String setInfo(String userInfo) {
        try {
            JSONObject jsonObject = new JSONObject(userInfo);
            image = jsonObject.getString("image");
            userId = jsonObject.getInt("id");
            username = jsonObject.getString("username");
            phoneNumber = jsonObject.getString("phoneNumber");
            isVip = jsonObject.getBoolean("isVip");
            balance = jsonObject.getDouble("balance");
            collectLocalTrial = jsonObject.getInt("collectLocalTrial");
            collectLpmsTrial = jsonObject.getInt("collectLpmsTrial");
            analysisTrial = jsonObject.getInt("analysisTrial");
            String tmp = jsonObject.optString("token");
            if (!tmp.isEmpty()) {
                token = tmp;
            }
            return tmp;
        } catch (JSONException e) {
            e.printStackTrace();
            return "";
        }
    }

    public String getInfo() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("analysisTrial", analysisTrial);
        map.put("balance", balance);
        map.put("collectLocalTrial", collectLocalTrial);
        map.put("collectLpmsTrial", collectLpmsTrial);
        map.put("enabled", enabled);
        map.put("id", userId);
        map.put("image", image);
        map.put("password", "");
        map.put("phoneNumber", phoneNumber);
        map.put("userName", username);
        map.put("vip", isVip);

        LogUtil.e("getInfo: map json:" + new JSONObject(map).toString());
        return new JSONObject(map).toString();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public boolean isVip() {
        return isVip;
    }

    public void setVip(boolean vip) {
        isVip = vip;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getCollectLocalTrial() {
        return collectLocalTrial;
    }

    public void setCollectLocalTrial(int collectLocalTrial) {
        this.collectLocalTrial = collectLocalTrial;
    }

    public int getCollectLpmsTrial() {
        return collectLpmsTrial;
    }

    public void setCollectLpmsTrial(int collectLpmsTrial) {
        this.collectLpmsTrial = collectLpmsTrial;
    }

    public int getAnalysisTrial() {
        return analysisTrial;
    }

    public void setAnalysisTrial(int analysisTrial) {
        this.analysisTrial = analysisTrial;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        UserManager.token = token;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

}
